package com.yiwenup.leetcode.offer;

import com.yiwenup.leetcode.ListNode;

/**
 * https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
 **/
public class No024 {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    /**
     * 执行用时：0 ms, 在所有 Java 提交中击败了100.00%的用户
     * 内存消耗：38.3 MB, 在所有 Java 提交中击败了26.56%的用户
     */
    public static ListNode reverseList2(ListNode head) {
        if (head == null || head.next == null) return head;

        ListNode pre = null;
        ListNode cur = head;

        while (head != null) {
            head = head.next;
            cur.next = pre;
            pre = cur;
            cur = head;
        }

        return pre;
    }
}
